package com.cloud.jucTest;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 正确理解ConcurrentHasp并发 会出现非50的情况
 */
public class ConcurrentHashMapTest {
    public static void main(String[] args) throws InterruptedException {
        Map<String, Integer> map = new ConcurrentHashMap<String, Integer>();
        map.put("key", 0);
        for (int i = 0; i < 50; ++i) {
            new Thread(() -> {
                Integer val = map.get("key");
                val++;
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                map.put("key", val);
            }).start();
        }
        Thread.sleep(3000);
        System.out.println("the finally value is " + map.get("key"));
    }
}
